package com.xb.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/data")
public class GetCodeController {

    /**
     * 获取js
     */
    @RequestMapping(value = "/getCode/{userid}/{siteid}")
    public void getCode(@PathVariable String userid, @PathVariable String siteid, HttpServletRequest request, HttpServletResponse res) {
        res.setContentType("text/javascript");
        PrintWriter out = null;
        try {
            out = res.getWriter();
            out.print("(function(){var win=window,doc=document;var lm_id=\""+userid+"\",site_id=\""+siteid+"\";var cookie={addCookie:function(objName,objValue,objDays){var str=objName+\"=\"+escape(objValue);if(objDays>0){var date=new Date();var ms=objDays*24*3600*1000;date.setTime(date.getTime()+ms);str+=\"; expires=\"+date.toGMTString()}if(objDays===Infinity){str+=\"; expires=Fri, 31 Dec 9999 23:59:59 GMT\"}str+=\"; path=/\";doc.cookie=str},get:function(name){var cname=name+'=';var arrCookie=doc.cookie.split(';');for(var i=0;i<arrCookie.length;i++){var cookie=arrCookie[i];while(cookie.charAt(0)==' ')cookie=cookie.substring(1);if(cookie.indexOf(name)!=-1)return cookie.substring(cname.length,cookie.length)}return''}};var arandDiv=Math.floor(Math.random()*1000000);document.write('<div id=\"'+arandDiv+'\" style=\"width: 0.001px;height: 0.001px;margin: 0;padding: 0;opacity: 0\"></div>');function isShow(){var itop=document.getElementById(arandDiv).getBoundingClientRect().top;var ise=document.documentElement.clientHeight;if(itop<=ise&&itop>0){return'true'}else{return'false'}}function url(){return encodeURIComponent(win.location.href)}function getScreenResolution(){return win.screen.width+\"*\"+win.screen.height}function GPU(){var canvas=doc.createElement('canvas'),gl=canvas.getContext('experimental-webgl'),debugInfo=gl.getExtension('WEBGL_debug_renderer_info');return gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL)}function title(){return doc.title}function uuid(){var s=[];var hexDigits=\"0123456789abcdef\";for(var i=0;i<36;i++){s[i]=hexDigits.substr(Math.floor(Math.random()*0x10),1)}s[14]=\"4\";s[19]=hexDigits.substr((s[19]&0x3)|0x8,1);s[8]=s[13]=s[18]=s[23]=\"\";var uuid=s.join(\"\");return uuid}var uuid=uuid();if(!cookie.get('UUID_XB')){cookie.addCookie('UUID_XB',uuid,Infinity)}function searchInformation(){var referrer,searchInformation;try{referrer=win.opener?win.opener.doc.location.href:doc.referrer}catch(e){referrer=doc.referrer}function content(r){var s=[\"wd\",\"p\",\"q\",\"keyword\",\"kw\",\"w\",\"key\",\"word\",\"query\",\"q1\",\"name\"];if(r!=\"\"&&r!=null){for(var i=0;i<s.length;i++){var re=new RegExp(\"[^1-9a-zA-Z]\"+s[i]+\"=\\([^&]*\\)\");var kk=r.match(re);if(kk!=null){return kk[1]}}}return\"\"}function engine(r){var regexp=/\\.(sogou|soso|baidu|google|youdao|yahoo|bing|118114|biso|gougou|ifeng|ivc|sooule|niuhu|biso)(\\.[a-z0-9\\-]+){1,2}\\//ig;if(r!=\"\"&&r!=null){return r.match(regexp)}return\"\"}searchInformation=referrer?{referrer:encodeURIComponent(referrer),content:encodeURIComponent(content(referrer)),engine:encodeURIComponent(engine(referrer))}:{referrer:encodeURIComponent(referrer)};return searchInformation}function isWeiXin(){var ua=window.navigator.userAgent.toLowerCase();if(ua.match(/MicroMessenger/i)=='micromessenger'){return'MicroMessenger'}else{return false}}var userAgent=window.navigator.userAgent;var isShow=isShow(),url=url(),screen=getScreenResolution(),GPU=GPU(),title=title(),isWeiXin=isWeiXin(),searchInformation=searchInformation();function __imgGet(){var __imgXb=new Image();__imgXb.src=\"http://data1.xinba.com/log/data?lm_id=\"+lm_id+\"&site_id=\"+site_id+\"&userAgent=\"+userAgent+\"&referrer=\"+searchInformation.referrer+\"&url=\"+url+\"&screen=\"+screen+\"&gpu=\"+GPU+\"&title=\"+title+\"&content=\"+searchInformation.content+\"&engine=\"+searchInformation.engine+\"&uuid=\"+cookie.get('UUID_XB')+\"&isShow=\"+isShow}__imgGet();var coord;var maxY=getElementTop(document.getElementById(arandDiv))+500;var minY=getElementTop(document.getElementById(arandDiv))-500;function getMousePos(event){var e=event||win.event;var scrollX=doc.documentElement.scrollLeft||doc.body.scrollLeft;var scrollY=doc.documentElement.scrollTop||doc.body.scrollTop;var x=e.pageX||e.clientX+scrollX;var y=e.pageY||e.clientY+scrollY;return{'x':x,'y':y}}function getElementTop(element){var actualTop=element.offsetTop;var current=element.offsetParent;while(current!==null){actualTop+=current.offsetTop;current=current.offsetParent}return actualTop}function loginEvent(ele,eventType,fn,isRepeat,isCaptureCatch){if(isCaptureCatch===undefined||typeof isCaptureCatch!=='boolean'){isCaptureCatch=false}if(isRepeat===undefined||typeof isRepeat!=='boolean'){isRepeat=true}if(ele.eventList===undefined){ele.eventList={}}if(isRepeat===false){for(var key in ele.eventList){if(key===eventType){return'该事件已经绑定过！'}}}if(ele.addEventListener){ele.addEventListener(eventType,fn,isCaptureCatch)}else if(ele.attachEvent){ele.attachEvent('on'+eventType,fn)}else{return false}ele.eventList[eventType]=true}})()");
        } catch (IOException e) {
        }
    }
}
